package Ajax;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;

public class EducationPage extends AbsPersonalPage {

	public EducationPage(String resumeId, HttpServletRequest request) {
		super(resumeId, request);
		mStrSubClassName = "resumeducation";
	}

	@Override
	public void execSql(Statement stmt, HttpServletRequest request)
			throws SQLException {
		if (bCreate) {
			return;
		}

		if (bSubmit) {

			String sql = "INSERT INTO education VALUES(null, " + mResumeId + ", "
					+ request.getParameter("dsyear") + ", "
					+ request.getParameter("dsmonth") + ", "
					+ request.getParameter("deyear") + ", "
					+ request.getParameter("demonth") + ", "
					+ (request.getParameter("property") == null ? "false" : "true") + ", '"
					+ request.getParameter("deducation") + "', '"
					+ request.getParameter("dschool1") + "', '"
					+ request.getParameter("dschool2") + "', '"
					+ request.getParameter("school") + "', '"
					+ request.getParameter("ddpa") + "', '"
					+ request.getParameter("ddpb") + "', '"
					+ request.getParameter("ddpc") + "', '"
					+ request.getParameter("edutxt") + "')";

			stmt.executeUpdate(sql);

			return;
		}

		if (bDelete) {
			String sql = "DELETE FROM education WHERE id = " + mId;
			
			stmt.executeUpdate(sql);
			
			return;
		}

		String sql = "SELECT id, syear, smonth, eyear, emonth, edu_prop, edu_bg, school, tschool2 FROM education WHERE ";

		if (nModify > 0) {
			sql += "id = " + nModify;
		} else {
			sql += "resume_id = " + mResumeId;
		}

		rs = stmt.executeQuery(sql);
	}

	@Override
	protected void writeWithoutCreate(PrintWriter writer) throws SQLException {
		if (rs == null) {
			return;
		}

		writer.print("<ol><li>属性: 公开, 您的简历可以被全部企业用户查看</li><li>属性: 隐藏, 企业用户不可以查看您的简历详情</li>"
				+ "</ol>"
				+ "<div class=\"topic_list\">"
				+ "<table cellpadding=\"0\" cellspacing=\"0\">"
				+ "<thead>"
				+ "<tr><td>属性</td><td>学历</td><td>教育时间</td><td>毕业学校</td><td>管理选项</td></tr>"
				+ "</thead>");

		int i = 0;

		while (rs.next()) {
			String school = rs.getString("school");

			if (school == null || school.length() < 1) {
				school = rs.getString("tschool2");
			}

			boolean is_public = rs.getBoolean("edu_prop");

			writer.println("<tr class=\"bg" + (++i % 2) + "\">");
			writer.println("<td><img src=\"./images/personal/property_"
					+ (is_public ? "y" : "n") + ".gif\" alt=\"属性: "
					+ (is_public ? "公开" : "隐藏") + "\" /></td>");
			writer.println("<td>" + rs.getString("edu_bg") + "</td>");
			writer.println("<td>从 " + rs.getString("syear") + "-"
					+ rs.getString("smonth") + " 到 " + rs.getString("eyear")
					+ "-" + rs.getString("emonth") + "</td>");
			writer.println("<td>" + school + "</td>");
			writer.println("<td><a href=\"#\" onclick=\"resumeducation('','','"
					+ rs.getString("id") + "');return false;\">修改</a> | ");
			writer.println("<a href=\"#\" onclick=\"if (confirm('您是否确定要删除该条教育经历\t')){resumeducation('','','','"
					+ rs.getString("id")
					+ "');return false;}else{return false;}\">删除</a></td>");
		}

		writer.println("</tr></table></div><table cellpadding=\"0\" cellspacing=\"0\" class=\"formtable\"><tr><th width=\"100\">&nbsp;</th>	<td>");
		writer.println("<input type=\"button\" name=\"create\" value=\"添加教育经历\" onclick=\"resumeducation('',true)\" class=\"submit\" />");
		writer.println("<input type=\"button\" name=\"nextstep\" value=\"跳到下一步\" onclick=\"resumeload('award')\" class=\"button\" />");
		writer.println("</td></tr></table><div id=\"resumextentform\"></div>");
	}

	@Override
	protected void writeWithCreate(PrintWriter writer) throws IOException {
		String form_title = "<form action=\"ResumeSys?action=personal&amp;option=resume&amp;extent=education&amp;info=" + mResumeId + "&amp;submit=1\" ";
		File file = new File("c:\\xml\\education.xml");

		if (file.exists()) {
			InputStream fs = new FileInputStream(file);
			BufferedReader br = new BufferedReader(new InputStreamReader(fs,
					"utf-8"));

			String Line = null;

			writer.print(form_title);

			while ((Line = br.readLine()) != null) {
				writer.println(Line);
			}

			br.close();
			fs.close();
		} else {
			file.createNewFile();
		}
	}
}
